//
//  DXWTagViewController.m
//  BuDeJie
//
//  Created by 淡馨慰 on 16/7/31.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWTagViewController.h"
#import "DXWTagCell.h"
#import "DXWTagItem.h"
#import "AFHTTPSessionManager+DXWManager.h"
#import <MJExtension/MJExtension.h>
#import <SVProgressHUD/SVProgressHUD.h>



@interface DXWTagViewController ()

/** 存放模型的数组 */
@property (nonatomic, strong) NSMutableArray *tagArray;

/** 任务管理者 -- weak就行,有AFN管理 */
@property (nonatomic, weak) AFHTTPSessionManager *manager;


@end

@implementation DXWTagViewController

static NSString *ID = @"tagCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"推荐标签";
    
    //注册tagCell
    [self.tableView registerNib:[UINib nibWithNibName:@"DXWTagCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:ID];
    
    
    //加载数据
    [self loadData];
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];    
    //取消系统的分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //设置tableView的背景颜色
    self.view.backgroundColor = [UIColor grayColor];
    
    //6.发送请求前,告知用户 -- SVProgressHUD
    [SVProgressHUD showWithStatus:@"正在加载数据,请稍等..."];
}
- (void)loadData
{
    //1. 创建会话管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager dxw_Manager];
    self.manager = manager;
    //2. Get请求
    /*
     请求参数:
     a	true	string	tag_recommend
     action	true	string	sub
     c	true	string	topic
     */
    NSDictionary *dict = @{
                           @"a":@"tag_recommend",
                           @"action":@"sub",
                           @"c":@"topic"
                           };

    
    //NSURLSessionDataTask *dataTask =
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(100 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [manager GET:dxw_baseURL parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        
        //6.1 加载成功时,移除
        [SVProgressHUD dismiss];
        
        //5.字典转模型
        self.tagArray = [DXWTagItem mj_objectArrayWithKeyValuesArray:responseObject];
//        NSLog(@"%@",self.tagArray);
        
        //成功获取数据后,刷新tableView
        [self.tableView reloadData];
        
        //3.写入plist文件
        //4.设计模型
//        [responseObject writeToFile:@"/Users/milkteacat/Desktop/淡/dxw大神班17期-2/0727项目/代码2--订阅标签/tag.plist" atomically:YES];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        //6.2 加载失败时,移除
        [SVProgressHUD dismiss];
    }];
//                });
}
-(void)viewWillDisappear:(BOOL)animated
{
    //6.3当网络不好时,用户切换界面,也得移除
    //    dispatch_async(dispatch_get_main_queue(), ^{
    //    });
    
    //让所有对象都执行 -- 所有任务都取消
    [self.manager.tasks makeObjectsPerformSelector:@selector(cancel)];
    
    [SVProgressHUD dismiss];
    NSLog(@"%s",__func__);
    [super viewWillDisappear:animated];
}
//-(void)dealloc
//{
//
//}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tagArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DXWTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
    //获取模型
    DXWTagItem *item = self.tagArray[indexPath.row];
    cell.item = item;
    return cell;
}
//设置Cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //还原setFrame中减去的高度
    return 70 + 10;
}



@end
